package com.edu.whty.controller;

import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.entity.base.Mould;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.service.MouldService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mould")
public class MouldController {

    private Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private MouldService mouldService;


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity list(HttpServletRequest request, HttpServletResponse response,
                               @RequestBody Mould mould) {
        try {
            List<Mould> list = mouldService.getMouldList(mould);
            Map<String, Object> mapList = new HashMap<String, Object>();
            mapList.put("list", list);
            return ResponseEntity.getSuccessResponse(mapList);
        } catch (Exception e) {
            // 捕捉异常构造返回信息
            logger.error("获取模板列表发生异常" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "获取模板列表异常");
        }
    }
}
